package com.cuz.daileetcode.utils;

import javax.swing.plaf.metal.MetalTheme;
import java.util.ArrayList;
import java.util.Arrays;

public class ArrayUtils {
    public static void print(int[][] arr) {
        if (arr == null || arr.length == 0 || arr[0].length == 0) {
            System.out.println("NULL ARRAY");
        } else {
            for (int[] ints : arr) {
                System.out.println(Arrays.toString(ints));
            }
        }
    }

    public static void print(int[] arr) {
        if (arr == null || arr.length == 0) {
            System.out.println("NULL ARRAY");
        } else {
            System.out.println(Arrays.toString(arr));
        }
    }

    public static void print(int[] arr, int from, int to) {
        if (arr == null || arr.length == 0) {
            System.out.println("NULL ARRAY");
        } else {
            StringBuilder str = new StringBuilder();
            for (int index = from; index < to; index++) {
                str.append(arr[index]).append(",");
            }
            System.out.println(str);
        }
    }

    public static void swap(int[] arr, int index1, int index2) {
        int temp = arr[index1];
        arr[index1] = arr[index2];
        arr[index2] = temp;
    }

    public static int findMin(int[] arr) {
        if (isBlank(arr)) {
            throw new IllegalArgumentException("数组为空");
        }
        int min = Integer.MAX_VALUE;
        for (int num : arr) {
            min = Math.min(num, min);
        }
        return min;
    }

    public static int findMax(int[] arr) {
        if (isBlank(arr)) {
            throw new IllegalArgumentException("数组为空");
        }
        int max = Integer.MIN_VALUE;
        for (int num : arr) {
            max = Math.max(num, max);
        }
        return max;
    }

    public static boolean isBlank(int[] arr) {
        return arr == null || arr.length == 0;
    }

    public static String toString(int[][] arr) {
        StringBuilder res = new StringBuilder();
        for (int[] temp : arr) {
            res.append(Arrays.toString(temp)).append("\n");
        }
        return res.toString();
    }


    public static int[] create1By(String str) {
        if (str == null || str.length() == 0) {
            return new int[0];
        }
        int lenNeed=1;
        if (str.endsWith("]")){
            lenNeed=0;
        }
        String[] split = str.substring(1, str.length() - lenNeed).split(",");
        ArrayList<Integer> list = new ArrayList<>();
        for (String s : split) {
            try {
                int i = Integer.parseInt(s.trim());
                list.add(i);
            } catch (NumberFormatException e) {

            }
        }
        int[] res = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            res[i] = list.get(i);
        }
        return res;
    }

    public static int[][] create2By(String str) {
        if (str == null || str.length() == 0) {
            return new int[0][0];
        }
        String[] split = str.substring(1, str.length() - 1).split("],");
        ArrayList<int[]> ints = new ArrayList<>();
        int max = 0;
        for (String s : split) {
            int[] by = create1By(s.trim());
            ints.add(by);
            max = Math.max(by.length, max);
        }
        int[][] res = new int[ints.size()][max];
        for (int i = 0; i < ints.size(); i++) {
            int[] row = ints.get(i);
            res[i] = row;
        }
        return res;
    }

    public static void main(String[] args) {
        //[[4,10,15,24,26], [0,9,12,20], [5,18,22,30]]
        int[][] by = create2By("[[4,10,15,24,26], [0,9,12,20], [5,18,22,30]]");
        print(by);
    }
}
